package com.tgy.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/***
 * @ClassName: GetClassInfo
 * @Description: 获得类的信息
 * @Auther: 送一一块二向箔
 * @Date: 17:39 2021/12/1
 * @version : V1.0
 */
public class GetClassInfo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        //Class c1 = Class.forName("com.tgy.reflection.User");
        Class c1 = User.class;
        //获得类的全名
        System.out.println(c1.getName());
        //获得类的简称
        System.out.println(c1.getSimpleName());

        /*
        获得类的属性
        */

        System.out.println("==========");
        //只能获得public属性
        Field[] fields = c1.getFields();
        for (Field field: fields) {
            System.out.println(field);
        }
        System.out.println("==========");
        //这个能获取全部的属性,private也能获取
        fields = c1.getDeclaredFields();
        for (Field field: fields) {
            System.out.println(field);
        }
        //获得指定属性的值
        Field name = c1.getDeclaredField("name");
        System.out.println(name);

        //获得类的方法
        System.out.println("==========");
        //能把继承的方法也打出来
        //获得本类及其父类的全部public方法
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println("正常的"+method);
        }
        //获得本类的所有方法
        Method[] declaredMethods = c1.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println("Declared:"+method);
        }

        //获得指定方法
        //重载
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        //获得指定的构造器
        //public
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        //public private
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println("@@@@@" + declaredConstructor);
        }
        Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
        System.out.println("-->"+declaredConstructor);

    }


}
